package com.model;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.ServletException;

public abstract class DataModel {
	
	protected ArrayList<String> contextVariables;
	
	 protected String datasource="picseek";
	 protected String url="jdbc:mysql://localhost/";
	 protected String username="root";
	 protected String password="picseek";
	 protected String driverName="com.mysql.jdbc.Driver";
	 
	 protected Statement statement;
	 
	   public Connection c;

	 protected  void connect() throws ServletException {
	        try {
	        	/*java.lang.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				System.out.println("Connecting to the database server.....");
				c= DriverManager.getConnection("jdbc:sqlserver://localhost\\SQLEXPRESS;user=sa;password=021208;database=picSEEK_DB");
				System.out.println("Connected.....");
				//c = DriverManager.getConnection("jdbc:mysql://localhost/picseek", "root", "");
	            */
	        	
				String conectionString=url+datasource;
	        	/////String conectionString=url+"tagbasedrecommender";
	            Class.forName(driverName).newInstance();
	            //c = DriverManager.getConnection(conectionString,username, password);
	            c = DriverManager.getConnection(conectionString,username,password);
	            //c = DriverManager.getConnection("jdbc:mysql://localhost/picseek", "root", "");
	            statement=c.createStatement();
				
	        } catch (Exception e) {
	            throw new ServletException(e);
	        }
	    }

	    protected ResultSet fetch(String sql) throws ServletException {
	        try {
	            if (c == null || c.isClosed()) {
	            	c=null;
	                connect();
	            }
	          return c.createStatement().executeQuery(sql);
	            
	          //return statement.executeQuery(sql);
	            
	            
	        } catch (Exception e) {
	            throw new ServletException(e);
	        }
	    }

	    protected  void save(String sql) throws ServletException {
	        try {
	            if (c == null || c.isClosed()) {
	            	c=null;
	                connect();
	            }
	            c.createStatement().executeUpdate(sql);
	        } catch (Exception e) {
	            throw new ServletException(e);
	        }
	    }
	    
	    @Override
	    public void finalize() throws Throwable {
	    	
	    	c.close();
	    	super.finalize();
	    }


		 public void setContextVariables(String contextVaribale)
		 {
				// TODO Auto-generated method stub
				contextVariables.add(contextVaribale);
		 }

		 public void setContextVariables(ArrayList<String> contextvariables) {
				// TODO Auto-generated method stub
				contextVariables=contextvariables;
			}
	    
		 public String[] getContextVariables()
		 {
			 Object [] objarray=contextVariables.toArray();
			 String [] contextArray=new String[objarray.length];
			
			 for(int i=0;i<objarray.length;i++)
				 contextArray[i]=objarray[i].toString();
			 
			 return contextArray;
		 }
		 
		 public ArrayList<String> getContextVariablesList()
		 {
			 return contextVariables;
		 }

		 public  String getImageURL(int imageID) throws Exception
			{
				ResultSet rs;
				String url=null;
				try {
				rs=fetch("select ImageURL from image where ImageID='"+imageID+"'");
				} catch (ServletException e) {
					System.out.println(e.getMessage());
					e.printStackTrace();
				throw e;
				}
				
				while(rs.next())
				{
					url=rs.getString("ImageURL");
				}
				return url;
				
				
			}
		 
		 
		 
		 public String getSrcImageURL(int imageID) throws Exception{
			 ResultSet rs;
				String url=null;
				try {
				rs=fetch("select SrcImageURL from image where ImageID='"+imageID+"'");
				} catch (ServletException e) {
					System.out.println(e.getMessage());
					e.printStackTrace();
				throw e;
				}
				
				while(rs.next())
				{
					url=rs.getString("SrcImageURL");
				}
				return url;
		 }
		 
		 
		 
			public  ResultSet getContextDetails(String keyword) throws Exception
			{
				ResultSet rs;
				
				String sqlString=null;
		        
		                Iterator<String> iter = contextVariables.iterator();
		              /*  while (iter.hasNext()) {
		                      String tempConVar = iter.next();
		                      if(iter.hasNext())sqlString += tempConVar + ",";
		                      else sqlString += tempConVar+",ImageID";
		                    }*/
		              
		                if(keyword!=null)
		                {
		                	sqlString = "select * from searchhistory where Keyword='"+keyword+"'";
		                }else
		                {
		                	sqlString = "select * from searchhistory";
		                }
		        try {
		        	rs=fetch(sqlString);
				} catch (ServletException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					throw e;
				}
				return rs;
				
			}
			
		 
		 public  abstract int saveUserFeedback(HashMap contextMap,long userID,
				 String keyword,String url,long imageId) throws Exception;
		 
		 

		 
}
